package com.company.admin.service.job.school;

import com.company.admin.common.myException.BizException;
import com.company.admin.common.util.StrAndListUtil;
import com.company.admin.dao.file.FileDao;
import com.company.admin.dao.job.school.SchoolJobDao;
import com.company.admin.domain.job.school.SchoolJobDomain;
import com.company.admin.entity.file.AttachFileEntity;
import com.company.admin.entity.job.school.SchoolJobEntity;
import com.company.admin.req.job.school.SchoolJobAddRequest;
import com.company.admin.req.job.school.SchoolJobDeleteRequest;
import com.company.admin.req.job.school.SchoolJobListRequest;
import com.company.admin.req.job.school.SchoolJobModifyRequest;
import com.company.admin.req.job.social.SocialJobAddRequest;
import com.company.admin.resp.job.school.SchoolJobListResponse;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.misc.ObjectInputFilter;

import java.util.*;


/**
 * @program: myProject
 * @description: ${description}
 * @author: zhaochenglin
 * @create: 2019-03-03 16:07
 **/
@Service
public class SchoolJobService {

    @Autowired
    private SchoolJobDao schoolJobDao;
    @Autowired
    private FileDao fileDao;


    @Transactional
    public void add(SchoolJobAddRequest request){

        checkAdd(request);
        schoolJobDao.insert(req2Entity(request));
    }
    private void checkAdd(SchoolJobAddRequest request){
        if (request == null){
            throw new BizException(209, "没有请求的实体");
        }
        if (StringUtils.isBlank(request.getSchoolJobAddress())){
            throw new BizException(209, "工作地址必须填写");
        }
        if (StringUtils.isBlank(request.getSchoolJobInfo())){
            throw new BizException(209, "工作详情必须填写");
        }
        if (StringUtils.isBlank(request.getSchoolJobName())){
            throw new BizException(209, "工作名称必须填写");
        }
    }

    private SchoolJobEntity req2Entity(SchoolJobAddRequest request){
        SchoolJobEntity entity = new SchoolJobEntity();
        entity.setSchoolJobPicture(StrAndListUtil.listToStr(request.getSchoolJobPicture()));
        entity.setSchoolJobName(request.getSchoolJobName());
        entity.setSchoolJobInfo(request.getSchoolJobInfo());
        entity.setSchoolJobAddress(request.getSchoolJobAddress());
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        return entity;
    }

    private void checkModify(SchoolJobModifyRequest request){
        if (request.getSchoolJobId() == null){
            throw new BizException(209, "请选择要修改的校园岗位");
        }
    }
    @Transactional
    public void modify(SchoolJobModifyRequest request){
        checkModify(request);
        SchoolJobEntity entity = new SchoolJobEntity();
        entity.setUpdateTime(new Date());
        entity.setSchoolJobAddress(request.getSchoolJobAddress());
        entity.setSchoolJobInfo(request.getSchoolJobInfo());
        entity.setSchoolJobName(request.getSchoolJobName());
        entity.setSchoolJobPicture(StrAndListUtil.listToStr(request.getSchoolJobPicture()));
        entity.setSchoolJobId(request.getSchoolJobId());
        schoolJobDao.modify(entity);
    }


    private Map<String, Object> req2ListParam(SchoolJobListRequest request){
        if (request.getLength() == null || request.getOffset() == null){
            throw new BizException(209, "分页参数不完整");
        }

        Map<String, Object> param = new HashMap<>();
        param.put("offset", request.getOffset());
        param.put("length", request.getLength());
        return param;
    }

    public SchoolJobListResponse list(SchoolJobListRequest request){
        Map<String, Object> map = req2ListParam(request);
        Long totalCount = schoolJobDao.totalCount();
        SchoolJobListResponse response = new SchoolJobListResponse();
        if (totalCount.equals(0L)){
            response.setTotalCount(totalCount);
            return response;
        }
        List<SchoolJobEntity> entities = schoolJobDao.list(map);
        List<SchoolJobDomain> domains = new ArrayList<>();
        for (SchoolJobEntity entity : entities){
            SchoolJobDomain domain = new SchoolJobDomain();
            List<AttachFileEntity> attachFileEntities = new ArrayList<>();
            List<Long> ids = StrAndListUtil.strToList(entity.getSchoolJobPicture());
            if (CollectionUtils.isEmpty(ids)){
                attachFileEntities = fileDao.selectByIds(ids);
            }
            domain.setAttachFileEntities(attachFileEntities);
            domain.setCreateTime(entity.getCreateTime());
            domain.setSchoolJobAddress(entity.getSchoolJobAddress());
            domain.setSchoolJobId(entity.getSchoolJobId());
            domain.setSchoolJobInfo(entity.getSchoolJobInfo());
            domain.setSchoolJobName(entity.getSchoolJobName());
            domain.setUpdateTime(entity.getUpdateTime());
            domains.add(domain);
        }

        response.setTotalCount(totalCount);
        response.setDomains(domains);
        return response;
    }

    @Transactional
    public void delete(SchoolJobDeleteRequest request){
        if (CollectionUtils.isEmpty(request.getSchoolJobIds())){
            throw new BizException(209, "请先选择要删除的岗位");
        }
        schoolJobDao.delete(request.getSchoolJobIds());
    }
}
